<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增纵向课题')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-apply-add">
        <input type="hidden" id="reAmount" name="reAmount">
        <input type="hidden" id="accountData" name="accountData">
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">申请人：</label>
                <div class="col-sm-8">
<!--                    <select name="userId" class="form-control m-b" disabled>-->
<!--                        <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${user.flag}"></option>-->
<!--                    </select>-->
                    <div th:each="user : ${sysUsers}" th:if="${user.flag}">
                        <input  class="form-control" type="text" th:value="${user.userName}" readonly>
                        <input name="userId" class="form-control" type="hidden" th:value="${user.userId}">
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">课题类型：</label>
                <div class="col-sm-8">
                    <select name="topType" class="form-control" th:with="type=${@dict.getType('sci_vertical_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">课题名称：</label>
                <div class="col-sm-8">
                    <input name="topName" class="form-control" type="text" required>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">课题编号：</label>
                <div class="col-sm-8">
                    <input name="topNumber" class="form-control" type="text" required>
                </div>
            </div>
        </div>

        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">项目金额(万)：</label>
                <div class="col-sm-8">
                        <input name="amount"  class="form-control" type="number" required>
                </div>
            </div>
        </div>


        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">申请日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input autocomplete="off" name="signingData" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">主持人：</label>
                <div class="col-sm-8">
                    <div th:each="user : ${sysUsers}" th:if="${user.flag}">
                        <input  class="form-control" type="text" th:value="${user.userName}" readonly>
                        <input name="firstPersonId" class="form-control" type="hidden" th:value="${user.userId}">
                    </div>
                </div>
            </div>
        </div>


        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">成员1：</label>
                <div class="col-sm-8">

                    <select name="secondPersonId" class="form-control m-b" required>
                        <option value="">---请选择--</option>
                        <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}"  th:if="${user.userId} != 1 and ${user.flag != true}" ></option>
                    </select>
                </div>
            </div>
        </div>






        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">成员2：</label>
                <div class="col-sm-8">
                    <select name="thirdPersonId" class="form-control m-b" required>
                        <option value="">---请选择--</option>
                        <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}"  th:if="${user.userId} != 1 and ${user.flag != true}" ></option>
                    </select>
                </div>
            </div>
        </div>

        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">成员3：</label>
                <div class="col-sm-8">
                    <select name="fourthPersonId" class="form-control m-b" required>
                        <option value="">--请选择--</option>
                        <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}"  th:if="${user.userId} != 1 and ${user.flag != true}" ></option>
                    </select>
                </div>
            </div>
        </div>


        <div class="col-xs-6">
            <div class="form-group">
                <label class="col-sm-4 control-label is-required">文件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="filing">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="filing" name="file" type="file">
                    </div>
                </div>
            </div>
        </div>
<!--        <div class="col-xs-6">-->
<!--            <div class="form-group">-->
<!--                <label class="col-sm-4 control-label is-required">备案表：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <input type="hidden" name="filing">-->
<!--                    <div class="file-loading">-->
<!--                        <input class="form-control file-upload" id="filing" name="file" type="file">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
    </form>
</div>
<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: select2-js" />

<script th:inline="javascript">
    var prefix = ctx + "system/apply_vertical"
    $("#form-apply-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var firstPersonId = document.querySelector('input[name="firstPersonId"]').value;
            var secondPersonId = document.querySelector('select[name="secondPersonId"]').value;
            var thirdPersonId = document.querySelector('select[name="thirdPersonId"]').value;
            var fourthPersonId = document.querySelector('select[name="fourthPersonId"]').value;
            var personIds = [firstPersonId, secondPersonId, thirdPersonId, fourthPersonId];
            // 检查是否有重复的值
            var uniqueIds = new Set(personIds);
            if (uniqueIds.size !== personIds.length) {
                alert("成员不能有重复的值");
                return false;
            }
            if ($("input[name='filing']").val() === "")
                return $.modal.alertError("请上传文件");
            $.operate.saveTab(prefix + "/add", $('#form-apply-add').serialize());
        }
    }


    $("input[name='signingData']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='validityDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });
    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload/zxkt',
        maxFileCount: 1,
        autoReplace: true,
        allowedFileExtensions: ["pdf"],
        slugCallback: function(filename) {
            if ($("input[name='topName']").val()===""){
                $(".file-upload").fileinput('clear');
                $.modal.alertError("请先输入课题名称");
                return false;
            }
            // 自定义文件名生成规则
            fileExtension = filename.split('.').pop();
            const year = new Date().getFullYear();
            const newName = year +"-"+ $("input[name='topName']").val() +"-"+ "备案表"+ "." + fileExtension;
            return newName;
        },
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })
</script>
</body>
</html>